import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

// 滑动器视图
class SliderListView extends StatelessWidget {
  final String title;
  final List<double> minList;
  final List<double> maxList;
  final RxList<double> values;
  final Function(List<double> value)? onChanged;
  // 是否显示整数
  final bool isInteger;
  // 小数点的个数
  final int decimalPlaces;

  const SliderListView({
    super.key,
    required this.title,
    required this.minList,
    required this.maxList,
    required this.values,
    this.onChanged,
    this.isInteger = false,
    this.decimalPlaces = 1,
  });

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: SingleChildScrollView(
        child: ClipRRect(
          borderRadius: BorderRadius.circular(8.w),
          child: Container(
            color: GrayColor.gray2,
            child: Padding(
              padding: EdgeInsets.only(
                left: CONTENT_MARGIN,
                right: CONTENT_MARGIN,
                top: 26.w,
                bottom: 10.w,
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  LxText(
                    title,
                    fontWeightEnum: LxFontWeightEnum.titleSmall,
                    color: Colors.white,
                  ),
                  SizedBox(height: 8.w),
                  Column(
                    children: [
                      ListView.separated(
                        shrinkWrap: true,
                        physics: const NeverScrollableScrollPhysics(),
                        itemCount: values.length,
                        separatorBuilder: (context, index) => Padding(
                          padding: EdgeInsets.only(
                            left: CONTENT_MARGIN * 2.5,
                          ),
                          child: Divider(
                            color: Colors.white.withOpacity(0.3),
                            height: 1.w,
                          ),
                        ),
                        itemBuilder: (context, index) {
                          return SizedBox(
                            height: 80.w,
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    ClipRRect(
                                      borderRadius: BorderRadius.circular(12.w),
                                      child: Container(
                                        width: 24.w,
                                        height: 24.w,
                                        color:
                                            TewaThemeColor.moto5.withOpacity(1),
                                        child: Center(
                                          child: LxText(
                                            "${index + 1}",
                                            fontWeightEnum:
                                                LxFontWeightEnum.bodyMedium,
                                            color: Colors.white,
                                          ),
                                        ),
                                      ),
                                    ),
                                    SizedBox(width: CONTENT_MARGIN),
                                    Expanded(
                                      child: Obx(
                                        () {
                                          // Force reactivity by accessing the value
                                          final currentValue = values[index];
                                          return LxSlider(
                                            min: minList[index],
                                            max: maxList[index],
                                            value: currentValue,
                                            onChanged: (value) {
                                              values[index] = value;
                                              if (onChanged != null) {
                                                onChanged!(values);
                                              }
                                            },
                                          );
                                        },
                                      ),
                                    ),
                                    SizedBox(width: CONTENT_MARGIN),
                                    Obx(
                                      () => LxText(
                                        "${isInteger ? values[index].toInt().toString() : values[index].toStringAsFixed(decimalPlaces)}%",
                                        fontWeightEnum:
                                            LxFontWeightEnum.titleMedium,
                                        color: LxCol.theme,
                                      ),
                                    )
                                  ],
                                ),
                                SizedBox(height: CONTENT_MARGIN / 2),
                                Padding(
                                  padding: EdgeInsets.only(
                                      left: CONTENT_MARGIN * 2.5,
                                      right: CONTENT_MARGIN * 2.5),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      LxText(
                                        "${isInteger ? minList[index].toInt().toString() : minList[index].toStringAsFixed(decimalPlaces)} %",
                                        fontWeightEnum:
                                            LxFontWeightEnum.bodyMedium,
                                        color: Colors.white,
                                      ),
                                      LxText(
                                        "${isInteger ? maxList[index].toInt().toString() : maxList[index].toStringAsFixed(decimalPlaces)} %",
                                        fontWeightEnum:
                                            LxFontWeightEnum.bodyMedium,
                                        color: Colors.white,
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          );
                        },
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
